package com.lgs.common.widgets.xlist;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.databinding.DataBindingUtil;

public class BaseListItem<T> implements View.OnClickListener {
    static final public int list_back = Color.parseColor("#FAFAFA");
    static final public int list_line = Color.parseColor("#FFFFFF");// E4EAE8

    public ViewGroup parent;//父视图 listView//
    public View itemView;//listitem
    public T data;//关联的数据
    public ViewDataBinding binding;

    private IOnClickItemListener onClickListener;//点击事件回调

    @SuppressLint("InflateParams")
    public BaseListItem(Context context, int layout, ViewGroup parent) {
        this.parent = parent;
        LayoutInflater inflater = LayoutInflater.from(context);

        //获取item布局的binding
        itemView = inflater.inflate(layout, parent, false);
        //itemView = inflater.inflate(layout, null);//
        if (itemView.getTag() != null) {
            binding = DataBindingUtil.inflate(inflater, layout, parent, false);
            itemView = binding.getRoot(); //获取布局
        }
    }

    public void setListItem(int position, T item, IOnClickItemListener onClickListener) {
        this.data = item;
        this.onClickListener = onClickListener;

        switchBkColor(position);
    }

    @Override
    public void onClick(View view) {
        onClickListener.clickItem(view);
    }

    public void switchBkColor(int position) {
        if (position % 2 == 0) {
            itemView.setBackgroundColor(list_line);
        } else {
            itemView.setBackgroundColor(list_back);
        }
    }
}
